package cn.gotom.svg;

import java.util.Iterator;

import android.graphics.Canvas;
import android.graphics.RectF;

/**
 * @author 裴绍国
 * @author <a href="mailto:peixere@gmail.com">裴绍国</a>
 */
public class Group extends RenderableElement
{

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private RectF rect;

	public Group()
	{
	}

	public void render(Canvas canvas) throws SVGException
	{
		if (!visible())
		{
			return;
		}
		beginLayer(canvas);
		Iterator<SVGElement> it = children.iterator();
		while (it.hasNext())
		{
			SVGElement ele = (SVGElement) it.next();
			if (ele instanceof RenderableElement)
			{
				RenderableElement rendEle = (RenderableElement) ele;
				rendEle.render(canvas);
			}
		}
		finishLayer(canvas);
	}

	public RectF getBoundingBox()
	{
		if (rect == null)
			calcBoundingBox();
		return rect;
	}

	public void calcBoundingBox()
	{
		RectF retRect = null;
		for (Iterator<SVGElement> it = children.iterator(); it.hasNext();)
		{
			SVGElement ele = (SVGElement) it.next();

			if (ele instanceof RenderableElement)
			{
				RenderableElement rendEle = (RenderableElement) ele;
				RectF bounds = rendEle.getBoundingBox();
				if (bounds != null)
				{
					if (retRect == null)
						retRect = bounds;
					else
						retRect.union(bounds);
				}
			}
		}
		rect = boundsToParent(retRect);
	}
}
